/**
 * Created by shidaiapp on 2017/9/29.
 */
import actionUrls from "../../../../api/ApiUrls"
import ApiBase from "../../../../api/moudles/ApiBase"
function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalDeliver.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDeliver.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tStatus.modalImage.isShow = false;
  tiyData.tStatus.modalUnReceived.isShow = false;
  tiyData.tParams.orderSerialNumber = '';
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOrder.getRefundList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "serialNumber": tiyData.tParams.searchParams.orderSerialNumber,
    "isProcess":tiyData.tParams.searchParams.isProcess
  }).then(function (response) {
    self.$Spin.hide();
    self.$ySpliceAll(tiyData.tData.packageAftersaleList);
    if (response.success) {
      let aftersalePage = response.result;
      tiyData.tPage.total = aftersalePage.total;
      tiyData.tPage.stepNumber = aftersalePage.pageSize;
      tiyData.tData.packageAftersaleList.length = 0;
      aftersalePage.list.forEach(
        function (item) {
          tiyData.tData.packageAftersaleList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 查询并显示包裹售后详情
 * @param self
 * @param tiyData
 * @param id
 */
export const getPackageAftersale = (self, tiyData, id, orderId) => {
  self.$Spin.show();
  self.$apiOrder.getPackageAftersale({
    "id": id,
    "orderId": orderId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let aftersale = response.result;
      tiyData.tData.packageAftersale.id = aftersale.id;
      tiyData.tData.packageAftersale.packageId = aftersale.packageId;
      tiyData.tData.packageAftersale.serialNumber = aftersale.serialNumber;
      tiyData.tData.packageAftersale.waybill = aftersale.waybill;
      tiyData.tData.packageAftersale.userName = aftersale.userName;
      tiyData.tData.packageAftersale.createtime = self.$tiyBJTime(aftersale.createtime);
      if(aftersale.processTime){
        tiyData.tData.packageAftersale.processTime = self.$tiyBJTime(aftersale.processTime);
      } else {
        tiyData.tData.packageAftersale.processTime = "";
      }
      tiyData.tData.packageAftersale.type = aftersale.type;
      if(aftersale.type == 101){
        tiyData.tData.packageAftersale.typeTitle = '已收到货物';
      }else if(aftersale.type == 100){
        tiyData.tData.packageAftersale.typeTitle = '未收到货物';
      }
      if(aftersale.result){
        tiyData.tData.packageAftersale.result = aftersale.result;
        tiyData.tData.packageAftersale.resultTitle = self.aftersaleProcessTypeDict.filter(itemDict => itemDict.value == aftersale.result)[0].name;
      }
      self.$ySpliceAll(tiyData.tData.packageAftersale.itemAftersales);
      if(aftersale.itemAftersales && aftersale.itemAftersales.length > 0){
        aftersale.itemAftersales.forEach(function (item) {
          tiyData.tData.packageAftersale.itemAftersales.push(item);
        });
      }
      tiyData.tStatus.modalDetail.isShow = true;
    }
  });
};

/**
 * 显示未收到货包裹售后服务处理
 * @param self
 * @param tiyData
 */
export const toUnReceived = (self, tiyData) => {
  self.$refs['paramUnReceived'].resetFields();
  var aftersale = tiyData.tData.packageAftersale;
  tiyData.tParams.unReceived.id = aftersale.id;
  tiyData.tParams.unReceived.packageId = aftersale.packageId;
  tiyData.tParams.unReceived.serialNumber = aftersale.serialNumber;
  tiyData.tStatus.modalUnReceived.isShow = true;
};

/**
 * 保存未收到货包裹售后服务处理结果
 * @param self
 * @param tiyData
 * @param name
 */
export const handlerUnReceived = (self, tiyData, name) =>{
  tiyData.tStatus.modalUnReceived.isLoading = true;
  self.$refs[name].validate((valid) => {
    if(valid){
      var param = tiyData.tParams.unReceived;
      self.$apiOrder.handlerUnreceivedPackage(param).then(function (response) {
          if (response.success) {
            self.$Message.success('处理成功!');
            tiyData.tStatus.modalUnReceived.isLoading = false;
            tiyData.tStatus.modalUnReceived.isShow = false;
            getPackageAftersale(self, tiyData, param.id);
            webList(self, tiyData);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
    } else {
      tiyData.tStatus.modalUnReceived.isLoading = false;
      self.$nextTick(function () {
        tiyData.tStatus.modalUnReceived.isLoading = true;
      });
    }
  });
};

/**
 * 显示单项产品售后服务处理
 * @param self
 * @param tiyData
 */
export const toAftersaleItem = (self, tiyData, aftersaleItem) => {
  self.$refs['paramAftersaleItem'].resetFields();
  tiyData.tParams.aftersaleItem.id = aftersaleItem.id;
  tiyData.tParams.aftersaleItem.orderItem = aftersaleItem.orderItem;
  tiyData.tParams.aftersaleItem.type = aftersaleItem.type;
  tiyData.tParams.aftersaleItem.cause = aftersaleItem.cause;
  tiyData.tParams.aftersaleItem.number = aftersaleItem.number;
  tiyData.tParams.aftersaleItem.packageAftersaleId = aftersaleItem.packageAftersaleId;
  tiyData.tParams.aftersaleItem.packageId = aftersaleItem.packageId;
  tiyData.tParams.aftersaleItem.orderId = aftersaleItem.orderId;
  tiyData.tParams.aftersaleItem.orderItemId = aftersaleItem.orderItemId;
  tiyData.tParams.aftersaleItem.result = aftersaleItem.result;
  tiyData.tParams.aftersaleItem.opinion = aftersaleItem.opinion;
  if(!tiyData.tParams.aftersaleItem.result){
    var refundAmount = aftersaleItem.orderItem.actualPayment * aftersaleItem.number / aftersaleItem.orderItem.amount + "";
    tiyData.tParams.aftersaleItem.tempRefundAmount = refundAmount.substring(0, refundAmount.indexOf(".") + 3);
  } else {
    tiyData.tParams.aftersaleItem.refundAmount = aftersaleItem.refundAmount;
  }
  if(aftersaleItem.certificate){
    tiyData.tParams.aftersaleItem.certificate = self.$toJson(aftersaleItem.certificate);
  }
  tiyData.tParams.aftersaleItem.processTime = aftersaleItem.processTime;
  tiyData.tStatus.modalAftersaleItem.isShow = true;
};

/**
 * 保存产品单项售后服务处理结果
 * @param self
 * @param tiyData
 * @param name
 */
export const handlerAftersaleItem = (self, tiyData, name) =>{
  tiyData.tStatus.modalAftersaleItem.isLoading = true;
  self.$refs[name].validate((valid) => {
    if(valid){
      var param = {};
      param.orderItemAftersales = tiyData.tParams.aftersaleItem;
      param.packageId = tiyData.tParams.aftersaleItem.orderItem.packageId;
      param.orderItemAftersales.orderItem = {};
      param.orderItemAftersales.certificate.length = 0;
      tiyData.tStatus.modalAftersaleItem.submitLoading = true;
      self.$apiOrder.handlerAftersaleItem(param).then(function (response) {
        tiyData.tStatus.modalAftersaleItem.submitLoading = false;
        if (response.success) {
          self.$Message.success('处理成功!');
          tiyData.tStatus.modalAftersaleItem.isLoading = false;
          tiyData.tStatus.modalAftersaleItem.isShow = false;
          getPackageAftersale(self, tiyData, param.orderItemAftersales.packageAftersaleId);
        } else {
          self.$Message.error(response.errorMessage);
        }
      });
    } else {
      tiyData.tStatus.modalAftersaleItem.isLoading = false;
      self.$nextTick(function () {
        tiyData.tStatus.modalAftersaleItem.isLoading = true;
      });
    }
  });
};
